package com.cy.pj.notice.web.interceptor;

import com.cy.pj.common.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

/**
 * SpringWeb模块定义的拦截器,
 * HandlerInterceptor是接口规范
 */
public class TimeInterceptor implements HandlerInterceptor {
    Logger log = LoggerFactory.getLogger(this.getClass());
    /**
     * 后端handler方法执行之前执行
     * @param request 请求对象
     * @param response 响应对象
     * @param handler 目标处理器对象
     * @return 是否对请求进行放行，继续执行后续handler方法
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("*****preHandle*****");
        LocalTime localTime = LocalTime.now();
        int hour = localTime.getHour();
        if (hour<=6 || hour>24){
            throw new ServiceException("24-6点为系统维护期：请明天9点重新尝试");
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
